package com.ying.tests.BBBbook.java8.Chapter2;

import com.ying.tests.BBBbook.java8.Chapter1.Apple;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

import java.util.ArrayList;
import java.util.List;

/**
 * 第二章：通过行为参数化传递代码
 */
public class Chapter2 {

    public interface ApplePredicate {
        boolean test (Apple apple);
    }
    public class AppleHeavyWeightPredicate implements ApplePredicate {
        @Override
        public boolean test(Apple apple) {
            return apple.getWeight() > 150;
        }
    }
    public class AppleGreenColorPredicate implements ApplePredicate {
        @Override
        public boolean test(Apple apple) {
            return "green".equals(apple.getColor());
        }
    }

    public static void main(String[] args) {
        prettyPrintApple(new GetAppleStr());
        // 线程的写法
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello world 1");
            }
        });
        t.start();
        Thread t2 = new Thread(() -> System.out.println("Hello world 2"));
        t2.start();
        // JavaFX
        Button button = new Button("Send");
        Label label = new Label();
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                label.setText("Sent !!");
            }
        });

        button.setOnAction((ActionEvent event) -> label.setText("Sent ??"));
    }

    // 自定义接口并实现，直接通过接口调用接口的实现
    public interface GetApple {
        String test(Apple apple);
    }
    public static class GetAppleStr implements GetApple {
        @Override
        public String test(Apple apple) {
            return apple.toString();
        }
    }
    public static void prettyPrintApple(GetApple p) {
        List<Apple> list = new ArrayList<>();
        list.add(new Apple("臭狗砸", 222, "red"));
        list.add(new Apple("香仔仔", 111, "green"));
        for (Apple apple : list) {
            String output = p.test(apple);
            System.out.println(output);
        }
    }

}
